package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.GraphInfoMapper;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    RoomInfoMapper roomInfoMapper;
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    RoomFacilityService roomFacilityService;
    @Autowired
    RoomLabelService roomLabelService;
    @Autowired
    RoomAttrValueService roomAttrValueService;
    @Autowired
    RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    RoomLeaseTermService roomLeaseTermService;
    @Autowired
    FacilityInfoService facilityInfoService;
    @Autowired
    LabelInfoService labelInfoService;
    @Autowired
    AttrValueService attrValueService;
    @Autowired
    AttrKeyService attrKeyService;
    @Autowired
    PaymentTypeService paymentTypeService;
    @Autowired
    LeaseTermService leaseTermService;
    @Autowired
    ApartmentInfoService apartmentInfoService;
    @Autowired
    GraphInfoMapper graphInfoMapper;
    @Autowired
    LeaseAgreementService leaseAgreementService;

    /**
     * 条件分页查询房间列表
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(page,queryVo);
    }

    /**
     * 保存或更新房间信息
     * @param roomSubmitVo
     */
    @Override
    @Transactional
    public void saveItem(RoomSubmitVo roomSubmitVo) {

        Long id = roomSubmitVo.getId();
        //判断是否新增，不是就先删除关系表中的数据
        if (id != null){
            deleteRoomMore(id);
        }

        //插入或修改房间信息
        this.saveOrUpdate(roomSubmitVo);

        //获取房间id
        Long rId = roomSubmitVo.getId();

        //新增图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfoList = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemId(rId);
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setName(graphVo.getName());
            graphInfoList.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfoList);

        //新增房间配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilityList = new ArrayList<>();
        facilityInfoIds.forEach(facilityInfoId -> {
            RoomFacility roomFacility = RoomFacility.builder().roomId(rId).facilityId(facilityInfoId).build();
            roomFacilityList.add(roomFacility);
        });
        roomFacilityService.saveBatch(roomFacilityList);

        //新增标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabelList = new ArrayList<>();
        labelInfoIds.forEach(labelInfoId -> {
            RoomLabel roomLabel = RoomLabel.builder().roomId(rId).labelId(labelInfoId).build();
            roomLabelList.add(roomLabel);
        });
        roomLabelService.saveBatch(roomLabelList);

        //添加属性值信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
        attrValueIds.forEach(attrValueId -> {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(rId).attrValueId(attrValueId).build();
            roomAttrValueList.add(roomAttrValue);
        });
        roomAttrValueService.saveBatch(roomAttrValueList);

        //添加支付方式关系
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
        paymentTypeIds.forEach(paymentTypeId -> {
            RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(rId).paymentTypeId(paymentTypeId).build();
            roomPaymentTypeList.add(roomPaymentType);
        });
        roomPaymentTypeService.saveBatch(roomPaymentTypeList);

        //添加房间租期信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
        leaseTermIds.forEach(leaseTermId -> {
            RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(rId).leaseTermId(leaseTermId).build();
            roomLeaseTermList.add(roomLeaseTerm);
        });
        roomLeaseTermService.saveBatch(roomLeaseTermList);

    }

    @Override
    @Transactional
    public RoomDetailVo selectDetailById(Long id) {

        //查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);

        //所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);

        //查询图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM.getCode());
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getIsDeleted,0);
        List<GraphInfo> graphInfoList = graphInfoService.list(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>();
        graphInfoList.forEach(graphInfo -> {
            GraphVo graphVo = GraphVo.builder().name(graphInfo.getName()).url(graphInfo.getUrl()).build();
            graphVoList.add(graphVo);
        });
        roomDetailVo.setGraphVoList(graphVoList);

        //查询配置信息
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getIsDeleted,0);
        List<RoomFacility> roomFacilities = roomFacilityService.list(roomFacilityLambdaQueryWrapper);
        List<FacilityInfo> facilityInfoList = new ArrayList<>();
        roomFacilities.forEach(roomFacility -> {
            facilityInfoList.add(facilityInfoService.getById(roomFacility.getFacilityId()));
        });
        roomDetailVo.setFacilityInfoList(facilityInfoList);


        //查询标签信息
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getIsDeleted,0);
        List<RoomLabel> roomLabels = roomLabelService.list(roomLabelLambdaQueryWrapper);
        List<LabelInfo> labelInfoList = new ArrayList<>();
        roomLabels.forEach(roomLabel -> {
            labelInfoList.add(labelInfoService.getById(roomLabel.getLabelId()));
        });
        roomDetailVo.setLabelInfoList(labelInfoList);

        //查询属性值信息
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getIsDeleted,0);
        List<RoomAttrValue> roomAttrValues = roomAttrValueService.list(roomAttrValueLambdaQueryWrapper);
        List<AttrValueVo> attrValueVoList = new ArrayList<>();
        roomAttrValues.forEach(roomAttrValue -> {
            AttrValueVo attrValueVo = new AttrValueVo();
            attrValueVo.setId(roomAttrValue.getAttrValueId());
            AttrValue attrValue = attrValueService.getById(roomAttrValue.getAttrValueId());
            attrValueVo.setName(attrValue.getName());
            attrValueVo.setAttrKeyId(roomAttrValue.getAttrValueId());
            attrValueVo.setAttrKeyName(attrKeyService.getById(attrValue.getAttrKeyId()).getName());
            attrValueVoList.add(attrValueVo);
        });
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        //查询支付方式信息
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getIsDeleted,0);
        List<RoomPaymentType> roomPaymentTypes = roomPaymentTypeService.list(roomPaymentTypeLambdaQueryWrapper);
        List<PaymentType> paymentTypeList = new ArrayList<>();
        roomPaymentTypes.forEach(roomPaymentType -> {
            PaymentType paymentType = paymentTypeService.getById(roomPaymentType.getPaymentTypeId());
            paymentTypeList.add(paymentType);
        });
        roomDetailVo.setPaymentTypeList(paymentTypeList);

        //查询租期信息
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getIsDeleted,0);
        List<RoomLeaseTerm> roomLeaseTerms = roomLeaseTermService.list(roomLeaseTermLambdaQueryWrapper);
        List<LeaseTerm> leaseTermList = new ArrayList<>();
        roomLeaseTerms.forEach(roomLeaseTerm -> {
            LeaseTerm leaseTerm = leaseTermService.getById(roomLeaseTerm.getLeaseTermId());
            leaseTermList.add(leaseTerm);
        });
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    /**
     * 根据id删除房间信息
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Long id) {

        //先判断房间是否有租约信息
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId,id);
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getIsDeleted,0);
        leaseAgreementLambdaQueryWrapper.in(LeaseAgreement::getStatus, 2,5,7);
        long count = leaseAgreementService.count(leaseAgreementLambdaQueryWrapper);

        if (count > 0){
            throw new LeaseException(ResultCodeEnum.ADMIN_AGREEMENT_EXITS);
        }

        deleteRoomMore(id);
        this.removeById(id);

    }


    @Transactional
    @Override
    public void deleteRoomMore(long id){
        //删除图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //删除房间配套信息
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

        //删除标签信息
        LambdaQueryWrapper<RoomLabel> labelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        labelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(labelLambdaQueryWrapper);

        //删除属性信息
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        //删除房间支付方式信息
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        //删除房间租期信息
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

    }
}




